﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace FileTranslator
{
    public class ConstraintHelper
    {
        public List<ConstraintHint> OtherConstraints { get; set; }
        public void AddConstraint(ConstraintHint constraint)
        {
            OtherConstraints.Add(constraint);
        }

        public void RemoveConstraint(ConstraintHint constraint)
        {
            OtherConstraints.Remove(constraint);
        }

        public ConstraintHelper()
        {
            OtherConstraints = new List<ConstraintHint>();
        }

        public virtual bool Verify(DataView dt)
        {
            bool result = true;
            foreach (ConstraintHint ch in OtherConstraints)
            {
                result &= ch.Verify(dt);
                if (result == false)
                {
                    return false;
                }
            }

            return true;
        }

        // 修正从position所指代的行及其之后的所有行
        public virtual bool Fix(DataView dt, int position = 0)
        {
            if (position < 0 || position > dt.Count)
            {
                throw new ArgumentOutOfRangeException("row index of out range");
            }

            bool result = true;
            foreach (ConstraintHint ch in OtherConstraints)
            {
                result &= ch.Fix(dt, position);
                if (result == false)
                {
                    return false;
                }
            }

            return true;
        }

        // position代表的是rd这一行在原表中的位置
        // 返回true表示并没有违反约束（Fix没有修改），false表示验证失败并进行了修改
        public virtual bool Fix(DataRow rd, int position)
        {
            bool result = true;
            foreach (ConstraintHint ch in OtherConstraints)
            {
                result &= ch.Fix(rd, position);
            }

            return result;
        }
    }

    public class FmtConstraintHelper : ConstraintHelper
    {
        public FmtConstraintHelper()
        {
            ConstraintHint ch = new AutoIncreaseConstraint("elem_no", 1, 1);
            AddConstraint(ch);
        }
    }

    public class FldConstraintHelper : ConstraintHelper
    {
        public FldConstraintHelper()
        {
        }
    }

    public class FldProcessConstraintHelper : ConstraintHelper
    {
        public FldProcessConstraintHelper()
        {
            ConstraintHint ch = new AutoIncreaseConstraint("func_index", 0, 1);
            AddConstraint(ch);
        }
    }

    public class ElemBussIdConstraintHelper : ConstraintHelper
    {
        public ElemBussIdConstraintHelper()
        {
        }
    }

    public class SourFldTpConstraintHelper : ConstraintHelper
    {
        public SourFldTpConstraintHelper()
        {
        }
    }
}
